package com.erp.activity

import android.annotation.SuppressLint
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.erp.R
import com.erp.util.Httputil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_iv.*
import kotlinx.android.synthetic.main.header_knoya1.*
import org.xutils.common.Callback
import org.xutils.image.ImageOptions
import org.xutils.x
import java.util.*

/**
 * Created by Administrator on 2016/11/28/028.
 * 作者：刘洋
 * 查看图片界面
 */
class ChaKanTuZhiAvtivity : BaseActivity() {

    var code = 0
    lateinit var url: List<String>
    private val a = "LingShi1Avtivity"
    var onum: String = ""
    var guihao : String = ""
    lateinit var updatedialog: AlertDialog
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_iv
    }

    @SuppressLint("SetTextI18n")
    override fun initCreate() {
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = "查看图纸"
        iv_header_left.setOnClickListener { finish() }
        val intent = intent
        url = ArrayList()
        url = intent.getStringArrayListExtra("url")!!
        val bundle = intent.extras;
        val set = bundle?.keySet();
        for (it in set!!.iterator()){
            if(it== "onum"){
                onum = intent.getStringExtra("onum")
            }
            if(it=="guihao"){
                guihao = intent.getStringExtra("guihao")
            }
        }
        if(onum=="" && guihao ==""){
            tv_header_title.text = "查看图纸"
        }else{
            tv_header_title.text = "单号$onum  柜号$guihao"
        }
        getImage1(url)
    }


    fun getImage1(url: List<String>) {
        updatedialog = Myutil.showupdatedialog(this@ChaKanTuZhiAvtivity, "正在加载", false)
        IvDanju.mwidth = sp.getInt("width", 0)
        IvDanju.mheight = sp.getInt("height", 0)
        val options: ImageOptions
        if (url.indexOf("https") != -1) {
            options = ImageOptions.Builder().setFadeIn(true).setParamsBuilder(Httputil.getop(this@ChaKanTuZhiAvtivity)).build()
        } else {
            options = ImageOptions.Builder().setFadeIn(true).setParamsBuilder(Httputil.getop(this@ChaKanTuZhiAvtivity)).build()
        }
        Log.d("TAAAG", "url=$url")
        for (i in url.indices) {
            x.image().loadDrawable(url[i], options, object : Callback.CacheCallback<Drawable> {
                override fun onCache(result: Drawable): Boolean {
                    updatedialog.dismiss()
                    val bd = result as BitmapDrawable
                    // 初始化图片资源
                    val bitmap = bd.bitmap
                    //         获得位图的高
                    val height = bitmap.height
                    // 设置 ImageView初始化显示的图片
                    IvDanju.setbitmap(bitmap, height)
                    return true
                }

                override fun onSuccess(result: Drawable) {
                    updatedialog.dismiss()
                    val bd = result as BitmapDrawable
                    // 初始化图片资源
                    val bitmap = bd.bitmap

                    //         获得位图的高
                    val height = bitmap.height
                    // 设置 ImageView初始化显示的图片
                    IvDanju.setbitmap(bitmap, height)
                }

                override fun onError(ex: Throwable, isOnCallback: Boolean) {
                    updatedialog.dismiss()
                }

                override fun onCancelled(cex: Callback.CancelledException) {

                }

                override fun onFinished() {

                }
            })
        }
    }
}
